package com.zmn.oms.business.impl.conf.ordertag;

import com.google.common.collect.Lists;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.conf.ordertag.ConfOrderTagBService;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTag;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTagCategory;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTagCategoryQuery;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTagQuery;
import com.zmn.oms.model.vo.conf.ordertag.ConfOrderTagVO;
import com.zmn.oms.services.interfaces.conf.ordertag.ConfOrderTagCategoryService;
import com.zmn.oms.services.interfaces.conf.ordertag.ConfOrderTagService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：订单标签
 *
 * @author liuying
 * @date 2021/05/06 17:03
 */
@Slf4j
@Service
public class ConfOrderTagBServiceImpl implements ConfOrderTagBService {

    @Resource
    private ConfOrderTagService confOrderTagService;
    @Resource
    private ConfOrderTagCategoryService confOrderTagCategoryService;

    @Override
    public List<ConfOrderTagVO> listPageByQuery(ConfOrderTagQuery query) {
        if (StringUtils.isNotBlank(query.getKeyWord())) {
            if (NumberUtils.isDigits(query.getKeyWord())) {
                query.setId(Integer.valueOf(query.getKeyWord()));
            } else {
                query.setLikeTagName(query.getKeyWord());
            }
        }
        query.setOrderField("sort");
        query.setDefaultOrder(1);
        List<ConfOrderTag> confList = confOrderTagService.listPageByQuery(query);
        // 获取所有分类
        List<ConfOrderTagCategory> confOrderTagCategories = confOrderTagCategoryService.listByQuery(new ConfOrderTagCategoryQuery());
        Map<Integer, ConfOrderTagCategory> confOrderTagCategoriesMap = confOrderTagCategories.stream().collect(Collectors.toMap(ConfOrderTagCategory::getCategId, (e -> e)));

        List<ConfOrderTagVO> voList = Lists.newArrayListWithExpectedSize(confList.size());

        // 转换分类名称
        confList.stream().forEach(confOrderTag -> {
            ConfOrderTagVO confOrderTagVO = BeanMapper.map(confOrderTag, ConfOrderTagVO.class);
            String categoryIds = confOrderTag.getCategoryIds();
            if (StringUtils.isNotBlank(categoryIds)) {
                String[] categIdArr = categoryIds.split(",");
                for (int i = 0; i < categIdArr.length; i++) {
                    String categId = categIdArr[i];
                    if (StringUtils.isNotBlank(categId)) {
                        ConfOrderTagCategory confOrderTagCategory = confOrderTagCategoriesMap.get(Integer.valueOf(categId));
                        categIdArr[i] = confOrderTagCategory == null ? categIdArr[i] : confOrderTagCategory.getCategName();
                    }
                }
                confOrderTagVO.setCategNames(StringUtils.join(categIdArr, ","));
            }

            voList.add(confOrderTagVO);
        });
        return voList;
    }

    @Override
    public void modifyConfById(ConfOrderTag conf) throws OmsBaseException {
        Objects.requireNonNull(conf.getId(), "ID不能为空");

//        ConfOrderTagQuery query = ConfOrderTagQuery.builder()
//                .notId(conf.getId())
//                .tagName(conf.getTagName()).build();
//        int count = confOrderTagService.countByQuery(query);
//        if (count > 1) {
//            throw new OmsBaseException("名称已存在");
//        }

        ConfOrderTag updateConf = ConfOrderTag.builder()
                .id(conf.getId())
                .status(conf.getStatus())
                .categoryIds(conf.getCategoryIds())
                .color(conf.getColor())
                .sort(conf.getSort())
                .tagName(conf.getTagName())
                .tagShortName(conf.getTagShortName())
                .remark(conf.getRemark())
                .updater(conf.getUpdater()).build();
        confOrderTagService.updateConfById(updateConf);
    }

    @Override
    public void modifyConfStatusById(ConfOrderTag conf) {
        Objects.requireNonNull(conf.getId(), "ID不能为空");

        ConfOrderTag updateConf = ConfOrderTag.builder()
                .id(conf.getId())
                .status(conf.getStatus())
                .updater(conf.getUpdater()).build();
        confOrderTagService.updateConfById(updateConf);
    }

    @Override
    public void modifyConfSortById(ConfOrderTag conf) {
        Objects.requireNonNull(conf.getId(), "ID不能为空");

        ConfOrderTag updateConf = ConfOrderTag.builder()
                .id(conf.getId())
                .sort(conf.getSort())
                .updater(conf.getUpdater()).build();
        confOrderTagService.updateConfById(updateConf);
    }

    @Override
    @Transactional
    public Integer addConf(ConfOrderTag conf) throws OmsBaseException {
        // 重复检查
//        ConfOrderTagQuery query = ConfOrderTagQuery.builder()
//                .tagName(conf.getTagName()).build();
//        int count = confOrderTagService.countByQuery(query);
//        if (count > 0) {
//            throw new OmsBaseException("名称已存在");
//        }

        return confOrderTagService.insertConf(conf);
    }
}
